var gev = (function() {
	var initData = null;
	
	$(function() {
		// addEvent orientation or resize
		(typeof window.orientationchange != 'undefined') ? jQuery(window).bind('orientation', function() {
			gev.resizeGev();
		})
		: jQuery(window).bind('resize', function() {
			gev.resizeGev();
		});
		
		var clickEvent='click';
		if(navigator.userAgent.match(/like Mac OS X/i)) clickEvent='touchstart';

		jQuery(document.body).on(clickEvent,'.btn_search',function() {
			gev.search_status('enable');
		});
		jQuery(document.body).on(clickEvent,'.btn_all_search, .close_search',function() {
			gev.search_status('disable');
		});

		jQuery(document.body).on(clickEvent,'.btn_side',function() {
			gev.panel_status('enable');
		});

		jQuery(document.body).on(clickEvent,'.panel .close',function() {
			gev.panel_status('disable');
		});

		jQuery(document.body).on(clickEvent,'.dimmed',function() {
			gev.panel_status('disable');
		});

		jQuery(document.body).on(clickEvent,'.game_score .tab a',function() {
			gev.tab(jQuery(this),'.game_score .tab a');
		});
		
		jQuery(document.body).on(clickEvent,'.use_social .btn_naver', function() {
			gev.switch_btn(jQuery(this),'naver');
		});
		
		jQuery(document.body).on(clickEvent,'.use_social .btn_facebook', function() {
			gev.switch_btn(jQuery(this),'facebook');
		});
		
		jQuery(document.body).on(clickEvent,'.member input, .member em',function() {
			gev.member_input(jQuery(this).parents('li'));
		});
		
		jQuery(document.body).on('keyup','.member input',function() {
			gev.input_status(jQuery(this),jQuery(this).parents('li'));
		});
		
		jQuery(document.body).on('focusout','.member input',function() {
			gev.input_status(jQuery(this),jQuery(this).parents('li'));
		});
		
		jQuery(document.body).on(clickEvent,'.member .delete',function() {
			gev.initial_input(jQuery(this).parents('li'));
		});
		
		jQuery(document.body).on(clickEvent,'.member li .email_agree',function() {
			gev.btn_chk(jQuery(this));
		});
		
		jQuery(document.body).on(clickEvent,'.member .agree1,.member .agree2',function() {
			gev.btn_chk(jQuery(this));
		});

		jQuery(document.body).on(clickEvent,'.member .submit_reset',function() {
			gev.initial_form();
		});

		$(document.body).on('mouseleave','.selectbox',function(){
			gev.select_close();
		});

		$(document.body).on(clickEvent,'.login_box .txt_nick',function() {
			//gev.loginbox(jQuery(this),'focus');
		});

		$(document.body).on('focusout','.login_box .txt_nick',function() {
			//gev.loginbox(jQuery(this),'focusout');
		});

		jQuery(document.body).on(clickEvent,'.find_user .tab a',function() {
			gev.tab(jQuery(this),'.find_user .tab a');
		});

		jQuery(document.body).on(clickEvent,'.mypage_tab .tab a',function() {
			gev.tab(jQuery(this),'.mypage_tab .tab a');
		});

		jQuery(document.body).on(clickEvent,'.online_wrap .tab a',function() {
			gev.tab(jQuery(this),'.online_wrap .tab a');
		});

		jQuery(document.body).on(clickEvent,'#pop_gamescore',function() {
			gev.popShow('.pop_gamescore', 'dimmed');
		});

		jQuery(document.body).on(clickEvent,'.pop_gamescore .pop_close',function() {
			gev.popHide('.pop_gamescore');
		});

		jQuery(document.body).on(clickEvent,'.msg_login a',function() {
			gev.msgHide('.msg_login');
		});

		jQuery(document.body).on(clickEvent,'.helpdesk li h4 a',function() {
			gev.help_view(jQuery(this),'.helpdesk li');
		});

		jQuery(document.body).on(clickEvent,'.counsel_tab .tab a',function() {
			gev.tab(jQuery(this),'.counsel_tab .tab a');
		});

		jQuery(document.body).on(clickEvent,'.counsel_list li h4 a.tit',function() {
			gev.help_view(jQuery(this),'.counsel_list li');
		});

		gev.touchScroll('panel')
		gev.resizeGev();
		gev.reply_ico();
		gev.search_hide();
		gev.search_show();
		gev.input_focus('.reply_write .input textarea');

	});
	
	return {
		resizeGev : function() {
			$('.dimmed').css({
				'width':$(window).width(),
				'height':$(window).height()
			});
			$('.panel').css({
				'height':$(window).height()
			});

			if(jQuery('.panel').hasClass('enable_panel')) {
				jQuery('.gev').css({
					'height':$(window).height(),
					'overflow':'hidden'
				});
			}
		},
		search_status : function(status) { 
			if(status=='enable') {
				jQuery('.before_search').hide();
				jQuery('.after_search').show();
			} else {
				jQuery('.before_search').show();
				jQuery('.after_search').hide();
			}
			
			return status;
		},
		panel_status : function(status) {
			if(status=='enable') {
				jQuery('.gev').css({
					'height':$(window).height(),
					'overflow':'hidden'
				});

				jQuery('.panel').addClass('enable_panel');
				jQuery('.dimmed').show();
				jQuery('.board_util').hide();
			} else {
				jQuery('.gev').css({
					'height':'auto',
					'overflow':'visible'
				});
				jQuery('.panel').removeClass('enable_panel');
				jQuery('.dimmed').hide();
				jQuery('.board_util').show();
			}

		},
		tab : function(that,cls) {
			jQuery(cls).removeClass('on');
			that.addClass('on');
			jQuery('.tab_contents .tabs').hide();
			jQuery('.tab_contents .'+that.attr('id')).show();
		},
		switch_btn : function(target,param) {
			if(target.hasClass('disable_'+param)) {
				target.removeClass('disable_'+param).addClass('enable_'+param);
			} else {
				target.addClass('disable_'+param).removeClass('enable_'+param);
			}
		},
		member_input : function(target) {
			target.find('em').hide();
			target.find('input').focus();
		},
		input_status : function(that,target) {
			if(that.val().length>0) {
				target.find('.delete').show();
				target.find('em').hide();
			} else {
				target.find('.delete').hide();
				target.find('em').show();
			}
		},
		initial_input : function(target) {
			target.find('input').val('');
			target.find('em').show();
			target.find('.delete').hide();
		},
		btn_chk : function(target) {
			if(target.hasClass('on')) {
				target.removeClass('on');
				target.find('input').attr('checked',false);
			} else {
				target.addClass('on');
				target.find('input').attr('checked',true);
			}
		},
		initial_form : function() {
			jQuery('.member div').removeClass('on');
			jQuery('.member input[type=checkbox]').attr('checked',false);
			jQuery('.member input[type=text]').val('');
			jQuery('.member em').show();
			jQuery('.member .delete,.member .warn').hide();
		},
		selview : function(id, result) {
			if ($("div#"+id+" .depth").css("display") == "none") { 
				$("div#"+id).addClass("zindex");
				$("div#"+id).parents('.auth_inner').css('border-radius','0 5px 0 0');
				$("div#"+id+" .depth").slideDown("fast", function(){
					$("div#"+id+" .depth").focus();
				});
			} else {
				$("div#"+id+" .depth").slideUp("fast", function(){
					$("div#"+id).removeClass("zindex");
				});
			}
			
			$("div#"+id+" .depth .value").click(function(){
				$("div#"+id+" .depth").slideUp("fast", function(){
					$("div#"+id).removeClass("zindex");
					$("div#"+id).parents('.auth_inner').css('border-radius','0 5px 5px 0');
				});
				
				var catename = $(this).html();
				$("div#"+id+" .tval").html(catename);
				var cateval = $(this).data("val");
				if (cateval == undefined) {
					cateval = catename;
				}
				$("input#"+result).val(cateval);
			});
		},
		select_close : function() {
			$(".selectbox .depth").slideUp("fast", function(){
				$(".selectbox").removeClass("zindex");
				$(this).parents('.auth_inner').css('border-radius','0 5px 5px 0');
			});
		},
		loginbox :function(target,param) {
			if(param=='focus') {
				target.parents('.nick_inner').css({
					'border-top':'1px solid #cdcdcd',
					'border-left':'1px solid #cdcdcd',
					'border-bottom':'1px solid #cdcdcd'
				});
			} else {
				target.parents('.nick_inner').css({
					'border-top':'1px solid #4cf4ff',
					'border-left':'1px solid #4cf4ff',
					'border-bottom':'1px solid #4cf4ff'
				});
			}
		},
		reply_ico : function() {
			jQuery('.reply_list > ul > li:odd').addClass('odd');
		},
		search_hide : function() {
		
			var clickEvent='click';
			if(navigator.userAgent.match(/like Mac OS X/i)) clickEvent='touchstart';

			jQuery('.board_util .btn_search').on(clickEvent,function(e){e.preventDefault();});
			jQuery('.board_util .btn_search').click(function() {
				jQuery('.board_util .search_wrap').toggle();
			});
			jQuery('.board_util .search_inner input').on({
				blur : function() {
					jQuery('.board_util .search_wrap').hide();
				}
			});
		},
		search_show : function() {
		
			var clickEvent='click';
			if(navigator.userAgent.match(/like Mac OS X/i)) clickEvent='touchstart';
			
			jQuery('.board_util li a:first-child').on(clickEvent,function(e){
				jQuery('.board_util .search_wrap').toggle();
			});
		},
		isTouchDevice : function(){
			try{
				document.createEvent("TouchEvent");
				return true;
			}catch(e){
				return false;
			}
		},
		touchScroll : function(id){
			if(gev.isTouchDevice()){ //if touch events exist...
				var el=document.getElementById(id);
				var scrollStartPos=0;

				document.getElementById(id).addEventListener("touchstart", function(event) {
					scrollStartPos=this.scrollTop+event.touches[0].pageY;
					event.preventDefault();
				},false);

				document.getElementById(id).addEventListener("touchmove", function(event) {
					this.scrollTop=scrollStartPos-event.touches[0].pageY;
					event.preventDefault();
				},false);
			}
		},
		popShow : function(obj, type) {
			if (type == 'dimmed')
			{
				jQuery('.dimmed_popup').show();
				jQuery(obj).show();
			}
			else {
				jQuery(obj).show();
			}
		},
		popHide : function(obj) {
			jQuery('.dimmed_popup').hide();
			jQuery(obj).hide();
		},
		msgHide : function(obj) {
			jQuery(obj).hide();
		},
		help_view : function(help_select, help_list) {
			help_select.parent().parent().toggleClass('on');
		},
		input_focus : function(position) {
			$(position).on({
				focus : function() {
					$(this).addClass('on val');
				},
				blur : function() {
					if ($(this).val() == '')
					{
						$(this).removeClass('on val');
					}
					else
					{
						$(this).removeClass('val');
					}
				}
			});
		}
	};
}());
